package com.Endurancer.manager.service.impl;

import java.util.List;
import com.Endurancer.common.utils.DateUtils;
import com.Endurancer.manager.domain.dto.VisitorDto;
import com.Endurancer.manager.domain.vo.BlogCountVo;
import com.Endurancer.manager.mapper.BlogPostMapper;
import com.Endurancer.manager.mapper.BlogTagsMapper;
import com.Endurancer.manager.mapper.BlogThemesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Endurancer.manager.mapper.SiteInformationMapper;
import com.Endurancer.manager.domain.SiteInformation;
import com.Endurancer.manager.service.ISiteInformationService;

/**
 * 博客站点资讯统计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-11
 */
@Service
public class SiteInformationServiceImpl implements ISiteInformationService
{
    @Autowired
    private SiteInformationMapper siteInformationMapper;
    @Autowired
    private BlogPostMapper blogPostMapper;
    /**
     * 查询博客站点资讯统计
     *
     * @param infoId 博客站点资讯统计主键
     * @return 博客站点资讯统计
     */
    @Override
    public SiteInformation selectSiteInformationByInfoId(Long infoId)
    {
        return siteInformationMapper.selectSiteInformationByInfoId(infoId);
    }

    /**
     * 查询博客站点资讯统计列表
     *
     * @param siteInformation 博客站点资讯统计
     * @return 博客站点资讯统计
     */
    @Override
    public List<SiteInformation> selectSiteInformationList(SiteInformation siteInformation)
    {
        return siteInformationMapper.selectSiteInformationList(siteInformation);
    }

    /**
     * 新增博客站点资讯统计
     *
     * @param siteInformation 博客站点资讯统计
     * @return 结果
     */
    @Override
    public int insertSiteInformation(SiteInformation siteInformation)
    {
        siteInformation.setCreateTime(DateUtils.getNowDate());
        return siteInformationMapper.insertSiteInformation(siteInformation);
    }

    /**
     * 修改博客站点资讯统计
     *
     * @param siteInformation 博客站点资讯统计
     * @return 结果
     */
    @Override
    public int updateSiteInformation(SiteInformation siteInformation)
    {
        siteInformation.setUpdateTime(DateUtils.getNowDate());
        return siteInformationMapper.updateSiteInformation(siteInformation);
    }

    /**
     * 批量删除博客站点资讯统计
     *
     * @param infoIds 需要删除的博客站点资讯统计主键
     * @return 结果
     */
    @Override
    public int deleteSiteInformationByInfoIds(Long[] infoIds)
    {
        return siteInformationMapper.deleteSiteInformationByInfoIds(infoIds);
    }

    /**
     * 删除博客站点资讯统计信息
     *
     * @param infoId 博客站点资讯统计主键
     * @return 结果
     */
    @Override
    public int deleteSiteInformationByInfoId(Long infoId)
    {
        return siteInformationMapper.deleteSiteInformationByInfoId(infoId);
    }

    @Override
    public int editInformation(SiteInformation siteInformation) {
        BlogCountVo blogCountVo = blogPostMapper.selectBlogCount();
        siteInformation.setArticleCount(blogCountVo.getBlogCount());
        siteInformation.setTagCount(blogCountVo.getTagCounts());
        siteInformation.setThemeCount(blogCountVo.getThemeCounts());
        siteInformation.setUpdateTime(DateUtils.getNowDate());
        // 调用辅助方法格式化字数
        siteInformation.setArticleWords(formatWordCount(blogCountVo.getBlogWords()));
        // 假设这里有一个方法来更新站点信息并返回更新结果
        return siteInformationMapper.updateSiteInformation(siteInformation);
    }

    @Override
    public int editVisitor(VisitorDto visitorDto) {
        // 对传入的 visitorDto 进行判空处理
        if (visitorDto == null) {
            // 可以根据实际情况选择返回特定值或者抛出异常
            // 这里选择返回 0 表示操作未执行
            return 0;
        }
        // 查询站点信息
        SiteInformation siteInformation = siteInformationMapper.selectSiteInformationByInfoId(1L);
        // 对查询结果进行判空处理
        if (siteInformation == null) {
            // 同样根据实际情况处理，这里返回 0
            return 0;
        }
        // 判断是否是新访客
        if (Boolean.TRUE.equals(visitorDto.getIsNewVisitor())) {
            int visitorCount = siteInformation.getVisitorCount() + 1;
            siteInformation.setVisitorCount(visitorCount);
        }
        // 判断是否是新访问
        else if (Boolean.TRUE.equals(visitorDto.getIsNewVisit())) {
            int visitCount = siteInformation.getVisitCount() + 1;
            siteInformation.setVisitCount(visitCount);
        }
        // 更新站点信息
        return siteInformationMapper.updateSiteInformation(siteInformation);
    }

    /**
     * 格式化字数显示
     * @param wordCount 字数
     * @return 格式化后的字符串
     */
    private String formatWordCount(Integer wordCount) {
        if (wordCount == null || wordCount <= 0) {
            return "0字";
        } else if (wordCount < 10000) {
            return wordCount + "字";
        } else {
            double wan = (double) wordCount / 10000;
            return String.format("%.2f万", wan);
        }
    }

}
